//
//  CFNavViewController.m
//  CFFinanceProject
//
//  Created by chunfeng on 2018/4/9.
//  Copyright © 2018年 xiaowen. All rights reserved.
//
#import "CFNavViewController.h"

@interface CFNavViewController ()

@end

@implementation CFNavViewController


- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}


+ (void)initialize {
    // 获得全局导航条(设置导航样式 主题)
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    // 设置导航栏标题颜色和字体大小
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    [navBar setTitleTextAttributes:attributes];
    
    // 在ios7.0之后,该属性不再影响导航栏背景颜色,但是还会印象上面UIBarButtonItem的颜色
    navBar.tintColor = [UIColor whiteColor];
    
    // 获得全局的UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    //设置UIBarButtonItem颜色和字体大小
    NSMutableDictionary *itemAttributes = [NSMutableDictionary dictionary];
    itemAttributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    itemAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [item setTitleTextAttributes:itemAttributes forState:UIControlStateNormal];
    
    // 设置导航栏背景
    [navBar setBackgroundImage:[DEF_COLOR_MAINCOLOR colorTransformToImage] forBarMetrics:UIBarMetricsDefault];
    //去掉视图下导航栏的阴影
    [navBar setShadowImage:[DEF_COLOR_MAINCOLOR colorTransformToImage]];
    
    /**
     //改变系统的导航栏
     UINavigationBar * navBar = [UINavigationBar appearance];
     
     //导航栏背影色[UIColor colorWithRed:238/255.0 green:70/255.0 blue:70/255.0 alpha:1]
     [navBar setBarTintColor:[UIColor redColor]];
     
     //导航栏标题为白色
     [navBar setBarStyle:UIBarStyleBlack];
     
     //导航栏返回条颜色
     navBar.tintColor = [UIColor whiteColor];
     
     
     //tab bar 颜色
     UITabBar * tabBar = [UITabBar appearance];
     tabBar.tintColor = [UIColor redColor];**/
    
}

-(id)initWithRootViewController:(UIViewController *)rootViewController{
    
    CFNavViewController * nvc = [super initWithRootViewController:rootViewController];
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = self;
    }
    nvc.delegate = self;
    return nvc;
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (navigationController.viewControllers.count == 1)
        self.currentShowVC = Nil;
    else
        self.currentShowVC = viewController;
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        return (self.currentShowVC == self.topViewController);
    }
    return YES;
}

// 拦截系统的push操作
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count>0) {
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithTarget:self action:@selector(back)];
    }
    
    [super pushViewController:viewController animated:animated];
}

/**
 *  返回上一个控制器
 */
-(void)back{
    [self popViewControllerAnimated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
